<?php
/**
* Copyright 2009 Waag Society <http://www.waag.org>
* This file is part of Annotrax.
*
* Annotrax is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License,
* or (at your option) any later version.
*
* Annotrax is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
* See the GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with Annotrax.
* If not, see http://www.gnu.org/licenses/gpl.html
*/

?>
<?php
/**
 * Frame controller: create a thumbnail using movie id and offset in seconds
 */
class FrameController extends Zend_Controller_Action{
	const MOVIE_PATH = '/public/movies';

	/**
	 * implemntation of controller init. gets called before anything else happens
	 * @return void
	 */
	public function init(){
		// disable the trigger of view helpers
	    $this->_helper->layout()->disableLayout();
	}

	/**
	 * Use __call instead of actionFunctions because they are more sort of
	 * parameters to the controlelr
	 *
	 * @todo This should be implemented as a router and an indexAction to avoid magic functions
	 *
	 * @param string $name Function name to be called
	 * @param mixed $arguments Arguments for the function
	 * @return void
	 */
	public function __call( $name, $arguments ){
		$request = $this->getRequest();
		$cache   = false;

		if( null == $request->id ){
			die();
		}

		$movie = new Default_Model_Movie( $request->id );
		$frame = new Default_Model_Frame( array('timeCode' => $request->timecode, 'movie' => $movie ) );

		if( ( $filename = $movie->filename ) && ! empty( $filename ) ){
			//only save if there's a file, don't save bogus
			$frame->save();
			$cache = true;
		}

		$this->imageOut( $frame, $request->action, $cache );
	}

	/**
	 * Output image, setting proper headers.
	 * do a 304 if the browser has already seen our image
	 *
	 * @param Default_Model_Frame $frame Frame model to use
	 * @param $type The content type ( thumbnail, vignette, etc... )
	 * @param $cache Should we check for browser caching or not
	 * @return void Exits PHP
	 */
    private function imageOut( $image, $type, $cache = true ){
        date_default_timezone_set('Europe/Amsterdam');
        $inserted = strtotime( $image->inserted );

        if( $cache && isset( $_SERVER['HTTP_IF_MODIFIED_SINCE'] ) ){
            $since = strtotime( $_SERVER['HTTP_IF_MODIFIED_SINCE'] );

            if( $since === $inserted ){
               header('Last-Modified: '.$_SERVER['HTTP_IF_MODIFIED_SINCE'],true,304);
               exit;
            }
        }

        header( 'Content-Type: ' . $image->mimeType );
        header( 'Content-length' .  strlen( $image->$type ) );
        header( 'Content-Disposition: inline; filename=frame.jpg');
        header( 'Last-Modified: ' . date(DATE_RFC822, $inserted ) );
        header( 'Expires: ' . date( DATE_RFC822, strtotime('+10 Year', $inserted ) ) );
        header( 'Cache-Control: public, max-age=10800, pre-check=10800');
        header( 'Pragma: cache');
        echo $image->$type;
        exit;
    }

}
